package crudfx.control;
import javafx.scene.*;
import javafx.scene.image.*;
import javafx.scene.paint.*;
import javafx.scene.input.*;
import javafx.scene.shape.*;
import crudfx.interop.*;
import crudfx.field.*;
import crudfx.control.*;
import java.util.*;
import java.text.*;
/**
 * @profile desktop
 * @author Sergey Surikov
 */
public class CalendarDays extends CustomNode{
    public var x:Number;
    public var y:Number;
    public var width:Number;
    public var height:Number;
    public var background:Color=Color.web("#000000");
    public var foreground:Color=Color.web("#ffffff");
    public var datetime:Number on replace {
        if(not lock)fillNumbers();
        var now:Calendar=Calendar.getInstance();
        now.setTimeInMillis(datetime);
        var monthNames = new DateFormatSymbols().getMonths();
        month=monthNames[now.get(Calendar.MONTH)];
        year=now.get(Calendar.YEAR);
        }
    public function nextYear():Void{
        var now:Calendar=Calendar.getInstance();
        now.setTimeInMillis(datetime);
        now.add(Calendar.YEAR,1);
        datetime=now.getTimeInMillis();
        }
    public function prevYear():Void{
        var now:Calendar=Calendar.getInstance();
        now.setTimeInMillis(datetime);
        now.add(Calendar.YEAR,-1);
        datetime=now.getTimeInMillis();
        }
    public function nextMonth():Void{
        var now:Calendar=Calendar.getInstance();
        now.setTimeInMillis(datetime);
        now.add(Calendar.MONTH,1);
        datetime=now.getTimeInMillis();
        }
    public function prevMonth():Void{
        var now:Calendar=Calendar.getInstance();
        now.setTimeInMillis(datetime);
        now.add(Calendar.MONTH,-1);
        datetime=now.getTimeInMillis();
        }
    public var action=function():Void{};
    public-read var month:String;
    public-read var year:Integer;
    var days:Integer[];
    var names:String[];
    var lock:Boolean=false;
    function fillNumbers(){
        for(i in [0 .. 41])days[i]=0;
        var now:Calendar=Calendar.getInstance();
        now.setTimeInMillis(datetime);
        now.set(Calendar.DAY_OF_MONTH,1);
        var firstDay:Integer=now.get(Calendar.DAY_OF_WEEK);
        var month:Integer=now.get(Calendar.MONTH);
        firstDay=firstDay-now.getFirstDayOfWeek();
        if(firstDay<0)firstDay = 6;
        var n:Integer=0;
        for(i in[0..5])for(k in[0..6]){
                if(n>=firstDay and month==now.get(Calendar.MONTH)){
                    days[n]=now.get(Calendar.DAY_OF_MONTH);
                    now.add(Calendar.DATE, 1);
                    }
                    n++;
                }
    }
    override function create():Node {
        for(i in [0 .. 41])insert i into days;
        fillNumbers();
        var now=Calendar.getInstance();
        var dayNames=new DateFormatSymbols().getShortWeekdays();
        while(now.get(Calendar.DAY_OF_WEEK) != now.getFirstDayOfWeek()) {
            now.add(Calendar.DATE, 1);
            }
        for (i in [0..6]) {
            insert dayNames[now.get(Calendar.DAY_OF_WEEK)] into names;
            now.add(Calendar.DATE, 1);
            }
        var g:Group=Group {
            translateX:bind x
            translateY:bind y
            content:[
                Group{
                    content:for(xx in [0..6]) for(yy in [0..5]) Group{
                        var isHover:Boolean=false;
                        translateX:xx*width/7
                        translateY:yy*height/7+20
                        onMouseEntered:function(e:MouseEvent):Void{isHover=true;}
                        onMouseExited:function(e:MouseEvent):Void{isHover=false;}
                        onMouseClicked:function(e:MouseEvent):Void{
                            var now:Calendar=Calendar.getInstance();
                            now.setTimeInMillis(datetime);
                            now.set(Calendar.DAY_OF_MONTH, days[yy*7+xx]);
                            lock=true;
                            datetime=now.getTimeInMillis();
                            lock=false;
                            action();
                            }
                        content:[
                            Rectangle{
                                width:width/7-2
                                height:height/7-1
                                fill:bind if(isHover) then background else Color.web("#000000",0.001)
                                visible:bind (days[yy*7+xx]>0)
                                }
                            ,UINode{
                                translateX:3
                                translateY:2
                                content:ArtLabel{
                                    bold:false
                                    size:10
                                    text:bind if(days[yy*7+xx]>0) then "{days[yy*7+xx]}" else ""
                                    color:foreground
                                    }
                                }
                            ]
                        }
                    }
                ,Group{
                    content:for(i in [0 .. 6])UINode{
                        translateX:i*width/7+3
                        content:ArtLabel{
                            size:11
                            bold:true
                            text:names[i]
                            color:background
                            }
                        }
                    }
                ]
            };
        return g;
        }
    }
